I'm currently coding a bot in discord.py that does mod commands like kick, etc. I'm trying to make a command that you can suggest something to the server like they make a channel called suggestion-channel, when they do the command it sends it to that channel. I have a command that sends a message to a specific channel for bot suggestions, but that's the closest I've gotten. below is the code that I have for the suggest command in cogs, yes I use command handling.
#commands.command()
async def suggest(self ,ctx , *, suggestion):
embed = discord.Embed(title=f"Suggestion from {ctx.author.name}", description=suggestion, color=discord.Color.blue())
embed.set_footer(text=f"Suggestion created by {ctx.author}")
channel = discord.utils.get(message.server.channels, name="suggestion-channel")
message = await channel.send(embed=embed)
await message.add_reaction("✅")
await message.add_reaction("❌")
await ctx.message.delete()
await ctx.send("Thank you for your suggestion!")
First of all, you're not getting the channel in an on_message event, or using a user's message to get the server's channels. You'll have to replace message with ctx. Secondly, ctx.server is no longer valid since discord.py's migration. It would now be known as ctx.guild, which you can read about in their docs.
channel = discord.utils.get(message.server.channels, name="suggestion-channel")
# change this to:
channel = discord.utils.get(ctx.guild.channels, name="suggestion-channel")
Otherwise, if you find that any of your other commands are not working, you may have not processed your on_message event. You can view questions on how to solve this:
Why does on_message stop commands from working?
await process_commands(message) [docs]
discord.py #bot.command() not running
Related
I am trying to make a command which locks the whole server in discord.py but when I use the command, it just locks the channel which I used it in. Please help me if you can, Here is my current code.
#client.command()
#commands.has_permissions(manage_channels=True)
async def lockdown(ctx):
await ctx.channel.set_permissions(ctx.guild.default_role,send_messages=False)
embedVar3 = discord.Embed(
title="Locked",
description=(f'***Server has been locked.***'),
colour=(0x0000ff))
await ctx.send(embed=embedVar3)
You should be able to loop through each channel in the guild can lock each channel individually. Looking at the documentation, there doesn't seem to be a method for locking down the entire server.
#client.command()
#commands.has_permissions(manage_channels=True)
async def lockdown(ctx):
for channel in ctx.guild.channels:
channel.set_permissions(ctx.guild.default_role, send_messages=False)
...
Documentation
Hopefully this isn't a duplicate post but I've searched around and can't find anything on how to do this specifically.
I'm trying to create a Discord bot which can delete messages sent by a specific author, in a specific channel, but also checks previously sent messages.
At the moment I have the below, which adds new messages from chosen user to a list, checks if they're duplicates and if they are, deletes them.
I want to know:
A: How can I make this bot Channel specific
B: Can I have this program check old messages in that channel too and also delete them if they're duplicates?
Thanks in advance for any help and if any further info required, please let me know.
import discord
TOKEN = ('MY_TOKEN_LIVES_HERE')
client = discord.Client()
messagesSeen = []
#client.event
async def on_message(message):
if "News_Bot" in str(message.author):
if message.content in messagesSeen:
await message.delete()
else:
messagesSeen.append(message.content)
client.run(TOKEN)
Yes you can do both of those things. Since you are using on_message event you can just use message.channel to also check that channel for previously sent messages. I would also advise to change news_bot to the ID of the bot. The thing is one you implement this and clear the channel history for those messages, it won't be necessary anymore as the bot will catch the messages as soon as they are sent. So the message history is kind of a one time thing, which you can just create a cmd and run it in that channel.
messagesSeen = []
#client.event
async def on_message(message):
if 1234345 is message.author.id:
if message.content in messagesSeen:
await message.delete()
async for message in message.channel.history(limit=100):
if message.content in messagesSeen:
await message.delete()
else:
messagesSeen.append(message.content)
I am programming a discord bot that will send an embed to a separate channel when someone uses a command to prevent abuse and so our admins can keep an eye on usage.
I have this code:
#client.command()
#has_permissions(manage_messages=True)
async def purge(ctx, amount=5):
await ctx.channel.purge(limit = amount + 1) # Plus one is so that it also deletes the players purge message
embed = discord.Embed(title="Purged Channel", description="", colour=0xe74c3c, timestamp=datetime.utcnow())
embed.set_author(name="CS Moderation", icon_url="https://cdn.discordapp.com/attachments/938857720268865546/938863881726615602/ca6.png")
embed.add_field(name="Staff Member", value=ctx.author.mention, inline=False)
embed.set_footer(text="Made by HeadOfTheBacons#6666", icon_url=None)
channel = client.get_channel("938898514656784404") #Change ID of logging channel if needed
await channel.send(embed = embed)
When I run this, I cannot get the embed to be sent to a different channel. I have tried checking other posts already but I have not had much luck at all. I have no errors when I use the command or try to run the bot. The point here is when someone uses the command to make a new embed, set up the preferences, specify the specific channel ID where I want this embed to be sent to, and have the computer send it off to that channel. However, it is not sending. Why is my code not working? What do I need to change in my code to make this send a message?
Check if you have an on_command_error function in your program, it could be the reason of your no error problem.
Also not that the embed.footer can't be None, and the client.get_channel requires an Integer.
So you can try out the following code, it should work:
from datetime import datetime
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions
client = commands.Bot(command_prefix="prefix")
#client.command()
#has_permissions(manage_messages=True)
async def purge(ctx, amount=5):
await ctx.channel.purge(limit=amount + 1) # Plus one is so that it also deletes the players purge message
embed = discord.Embed(title="Purged Channel", description="", colour=0xe74c3c, timestamp=datetime.utcnow())
embed.set_author(name="CS Moderation", icon_url=client.user.avatar_url)
embed.add_field(name="Staff Member", value=ctx.author.mention, inline=False)
embed.set_footer(text="Made by HeadOfTheBacons#6666")
channel = client.get_channel(938898514656784404) # Change ID of logging channel if needed
await channel.send(embed=embed)
client.run("TOKEN")
I just recently started using cogs/extension files for Discord.py and ran into some issue that I don't know how to fix. I'm trying to send a message in a specific channel but I always just get the error AttributeError: 'NoneType' object has no attribute 'send'. I know that I can send a message in a specific channel with:
#client.command()
async def test(ctx):
await ctx.send("test")
channel = client.get_channel(1234567890)
await channel.send("test2")
And this works perfectly fine in my "main file", but not in my "extension file", so it's not because of a wrong ID. The await ctx.send("test") works without problems as well, together with any other command I have, just the channel.send is causing troubles.
I'm importing the exact same libraries & co and otherwise should have the exact same "setup" in both files as well.
NoneType error means that it doesn't correctly recognize the channel. When you use get_channel you are looking for the channel in the bot's cache which might not have it. You could use fetch_channel instead - which is an API call.
#client.command()
async def test(ctx):
await ctx.send("test")
channel = await client.fetch_channel(1234567890)
await channel.send("test2")
As you know, the error occurs because your channel is not recognized. The solution is fetch_channel(channel_id). The problem lies in your channel = client.get_channel (1234567890).
Try adding await before client. Next replace get_channel with fetch_channel.
For general use consider your get_channel(), instead you need fetch_channel (channel_id) to retrieve an x.GuildChannel or x.PrivateChannel with the specified ID.
#client.command()
async def test(ctx):
await ctx.send("test")
channel = await client.fetch_channel(1234567890) #update
await channel.send("test2")
I'm trying to make my discord bot disconnect the voice channel with a command like -disconnect using Lavalink. I have tried doing it in various approaches but it never seems to work for me. I also can't find many examples online. The most recent thing i've tried is this:
#commands.command()
async def leave(self, ctx):
guild_id = int(event.player.guild_id)
await self.connect_to(guild_id, None)
Tell me if you know how to make a disconnect command. Thanks!!!
This is discord.py rewrite btw.
To make a disconnect command is lavalink all you have to do is:
await self.connect_to(ctx.guild.id, None)
Here's an example:
import asyncio
#client.command()
async def disconnect(ctx):
channel = client.get_channel(channelIdHere)
vc = await channel.connect()
await asyncio.sleep(2) # Waits 2 seconds before leaving the vc
await vc.disconnect()
You should check the documentation for more information.