Discord.py –> channel.mention
Question:
I’m trying to make a bot for a discord server that simply listens for specific messages, deletes them and then refers the user to a different text channel (in a clickable link by mentioning it)
Here’s what I have now:
import Discord
import asyncio
client = discord.Client()
@client.event
async def on_message(message):
msg = '{0.author.mention}nWrong text channelnUse '.format(message)
if message.content.startswith('!p'):
await client.delete_message(message)
await client.send_message(message.channel, msg)
return
client.run('')
Ideally, I’d also want to search through a list with startswith()
instead of just ('!p')
& to ignore all messages from a specific text channel as well but I’m not sure how to do those either
Answers:
Sure, just add text_channel = client.get_channel('1234567890')
and reference its mention with text_channel.mention
(where 1234567890
is the id of the channel you want to link to)
So the code would end up looking something like this
@client.event
async def on_message(message):
text_channel = client.get_channel('1234567890')
msg = '{0.author.mention}nWrong text channelnUse {1.mention}'.format(message,text_channel)
if message.content.startswith('!p'):
await client.delete_message(message)
await client.send_message(message.channel, msg)
return
Regarding your second question, you could do something like this
arr = ['!p','!a','!b']
for a in arr:
if message.content.startswith(a):
break
else:
return
and remove the if message.content.startswith('!p'):
altogether
To ignore a specific channel just do if message.channel.id == "9876543210":
at the top of the function (9876543210
is the id of the channel you want to ignore commands from)
With those changes the code looks like this
@client.event
async def on_message(message):
if message.channel.id == "9876543210":
return
arr = ['!p','!a','!b']
for a in arr:
if message.content.startswith(a):
break
else:
return
text_channel = client.get_channel('1234567890')
msg = '{0.author.mention}nWrong text channelnUse {1.mention}'.format(message,text_channel)
await client.delete_message(message)
await client.send_message(message.channel, msg)
return
Don’t know if your problem is solved or not, but for all future developers looking on this thread. A super simple way to get a bot to mention a channel is like this…
<#channelID>
were the channel ID is the discord ID of the specific channel you wish to mention.
and in an example
await message.channel.send("Please go to <#channelID>")
I’m honestly a bit confused that no one on this thread has already mentioned this, so I feel like I’m missing something that y’all already know lol.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='>')
@client.event
async def on_ready():
print("Log : "+str(client.user))
@client.command()
async def mention(ctx):
ch = await client.fetch_channel(enter channel id)
await ctx.send(f"Mention your channel -> {ch}")
client.run("token")
I’m trying to make a bot for a discord server that simply listens for specific messages, deletes them and then refers the user to a different text channel (in a clickable link by mentioning it)
Here’s what I have now:
import Discord
import asyncio
client = discord.Client()
@client.event
async def on_message(message):
msg = '{0.author.mention}nWrong text channelnUse '.format(message)
if message.content.startswith('!p'):
await client.delete_message(message)
await client.send_message(message.channel, msg)
return
client.run('')
Ideally, I’d also want to search through a list with startswith()
instead of just ('!p')
& to ignore all messages from a specific text channel as well but I’m not sure how to do those either
Sure, just add text_channel = client.get_channel('1234567890')
and reference its mention with text_channel.mention
(where 1234567890
is the id of the channel you want to link to)
So the code would end up looking something like this
@client.event
async def on_message(message):
text_channel = client.get_channel('1234567890')
msg = '{0.author.mention}nWrong text channelnUse {1.mention}'.format(message,text_channel)
if message.content.startswith('!p'):
await client.delete_message(message)
await client.send_message(message.channel, msg)
return
Regarding your second question, you could do something like this
arr = ['!p','!a','!b']
for a in arr:
if message.content.startswith(a):
break
else:
return
and remove the if message.content.startswith('!p'):
altogether
To ignore a specific channel just do if message.channel.id == "9876543210":
at the top of the function (9876543210
is the id of the channel you want to ignore commands from)
With those changes the code looks like this
@client.event
async def on_message(message):
if message.channel.id == "9876543210":
return
arr = ['!p','!a','!b']
for a in arr:
if message.content.startswith(a):
break
else:
return
text_channel = client.get_channel('1234567890')
msg = '{0.author.mention}nWrong text channelnUse {1.mention}'.format(message,text_channel)
await client.delete_message(message)
await client.send_message(message.channel, msg)
return
Don’t know if your problem is solved or not, but for all future developers looking on this thread. A super simple way to get a bot to mention a channel is like this…
<#channelID>
were the channel ID is the discord ID of the specific channel you wish to mention.
and in an example
await message.channel.send("Please go to <#channelID>")
I’m honestly a bit confused that no one on this thread has already mentioned this, so I feel like I’m missing something that y’all already know lol.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='>')
@client.event
async def on_ready():
print("Log : "+str(client.user))
@client.command()
async def mention(ctx):
ch = await client.fetch_channel(enter channel id)
await ctx.send(f"Mention your channel -> {ch}")
client.run("token")