My code is returning this error NoneType object has no attribute 'send'
here is my code
import discord
import os
from discord.ext import commands
client = discord.Client()
class Logging(commands.Cog):
"""Sets up logging for you guild"""
def __init__(self, client):
self.bot = client
async def __error(self, ctx, error):
if isinstance(error, commands.BadArgument):
await ctx.send(error)
#commands.Cog.listener()
async def on_message_delete(self, message,):
deleted = embed = discord.Embed(
description=f"Message deleted in {message.channel.mention}", color=0x4040EC
).set_author(name=message.author, url= discord.Embed.Empty, icon_url=message.author.avatar_url)
channel = client.get_channel(888600482317213786)
deleted.add_field(name="Message", value=message.content)
deleted.timestamp = message.created_at
await channel.send(embed=deleted)
def setup(client):
client.add_cog(Logging(client))
I am doing this in my cogs and not in the main.py
channel = client.get_channel(888600482317213786) should be channel = self.bot.get_channel(888600482317213786). Then check if channel is None.
I assume there is no indentation error in your actual code.
Related
I have a problem with my code from my discord bot. When I run the program I get an error message, but I have all imports up to date. Can someone help me please, because I really have no idea how to fix this . If you have any more questions just write in.
Here is the error:
AttributeError: module 'discord' has no attribute 'ui'
And Here is my Code:
import discord
from discord.ext import commands
from discord_ui import ButtonInteraction, Button, ButtonStyle, UI
intent = discord.Intents.default()
intent.members = True
bot = commands.Bot(command_prefix="!", intent=intent)
role_id = 938465872098512956
guild_id = 938215477157703771
class RoleButton(discord.ui.Button):
def __init__(self):
super().__init__(
label="Verifiziere dich hier!",
style=discord.enums.ButtonStyle.blurple,
custom_id="interaction:RoleButton",
)
async def callback(self, interaction: discord.Interaction):
user = interaction.user
role = interaction.guild.get_role(role_id)
if role is None:
return
if role not in user.roles:
await user.add_roles(role)
await interaction.response.send_message(f"🎉 Du bist nun verifiziert!", ephemeral=True)
else:
await interaction.response.send_message(f"❌ Du bist bereits verifiziert!", ephemeral=True)
BILD_URL = ""
BESCHREIBUNG = "Test"
#bot.command()
async def post(ctx: commands.Context): # Command
view = discord.ui.View(timeout=None)
view.add_item(RoleButton())
await ctx.send(f"{BILD_URL}")
await ctx.send(f"{BESCHREIBUNG}", view=view)
#bot.event
async def on_ready():
print("ONLINE!")
view = discord.ui.View(timeout=None)
view.add_item(RoleButton())
bot.add_view(view)
bot.run("")
import discord
from discord.ext import commands
class AntiCog(commands.Cog):
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_message(self, message):
if message.author.id == 1234567891234567:
mention = f'<#!1234567891234567>'
if message.content == mention:
await message.channel.send("grow up")
user = message.author
print(str(user))
print(str(message.content))
muted_role = discord.utils.get(message.guild.roles, name="Muted")
await user.add_roles(muted_role)
else:
return
await self.client.process_commands(message)
def setup(client):
client.add_cog(AntiCog(client))
This's a working code for muting a person if they ping another person, however, I would like to make it a timed mute for 5 min. All of the resources I found were on_command timed mute, however, this's an auto one, how can I do so. thank you!
All you would have to do is add asyncio.sleep, and then remove the role, so:
import discord
from discord.ext import commands
import asyncio
class AntiCog(commands.Cog):
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_message(self, message):
if message.author.id == 1234567891234567:
mention = f'<#!1234567891234567>'
if message.content == mention:
await message.channel.send("grow up")
user = message.author
print(str(user))
print(str(message.content))
muted_role = discord.utils.get(message.guild.roles, name="Muted")
await user.add_roles(muted_role)
await asyncio.sleep(300) # you can change the time here
await user.remove_roles(muted_role)
else:
return
await self.client.process_commands(message)
def setup(client):
client.add_cog(AntiCog(client))
Be sure to import asyncio!
from aiogram import Bot
import config
def send_message(message):
operator = Bot(config.operator_token)
operator.send_message(config.user_id, message)
def main():
send_message('hi')
I want to send message to user with id from my config.py (config.user_id). But it's not workinkg. I tried many ways for it but I always got an error.
For example like this
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f94a9b62550>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7f94a98f8040>,
23065.38)]']
connector: <aiohttp.connector.TCPConnector object at 0x7f94a9b3c400>'
If you use aiogram you must use async/await to send message.
bot = Bot(token=TOKEN)
dp = Dispatcher(bot_init)
#dp.message_handler(commands=['test'])
async def process_start_command(message: types.Message):
await bot.send_message(message.from_user.id, "test message") #like this
#await message.answer("test message")
#or like this
Maybe this?!
import asyncio
from aiogram import Bot
import config
async def send_message(message):
operator = Bot(config.operator_token)
await operator.send_message(config.user_id, message)
def main():
asyncio.run(send_message('hi'))
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from config import TOKEN
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
#dp.message_handler(commands=['start'])
async def process_start_command(message: types.Message):
await message.reply("Hello\nWrite me something!")
#dp.message_handler(commands=['help'])
async def process_help_command(message: types.Message):
await message.reply("Implementation EchoBot")
#dp.message_handler()
async def echo_message(msg: types.Message):
await bot.send_message(msg.from_user.id, msg.text)
if __name__ == '__main__':
executor.start_polling(dp)
i tried to make cog for organize my bot but i have an error that i dont find how to fix. The bot successful find command in cog but when i write the command i have this error:
Could you help me pls ? Here is my code:
import discord
import asyncio
import re
import os
import random
from discord.ext import commands
class Moderation(commands.Cog):
def __init__(self, bot):
self.bot = bot
#Purge
#commands.command()
async def purge(ctx, amount=10):
await ctx.channel.purge(limit=amount)
Your first parameter must be self.
class Moderation(commands.Cog):
def __init__(self, bot):
self.bot = bot
#Purge
#commands.command()
async def purge(self, ctx, amount=10):
await ctx.channel.purge(limit=amount)
Add self as parameter in purge function.
async def purge(self,ctx,amount=10):
await ctx.channel.purge(limit=amount)
This is working perfectly without a COG, But in the COG It's not working and generating the following errors:
NameError: name 'bot' is not defined
import discord
from discord.ext import commands
class channelinfo(commands.Cog):
##commands.Cog.listener() [EVENT]
##commands.command() [COMMAND]
def init(self, bot):
self.bot = bot
#commands.command()
async def channelinfo(self,ctx,*,val:str = None):
val = val.replace('<','')
val = val.replace('>','')
val = val.replace('#','')
print(val)
channel = await bot.get_channel(int(val))
Even the discord.User is not working in the COG.
Error: Command raised an exception: AttributeError: 'User' object has no attribute 'roles'
#commands.command()
async def userinfo(self,ctx,user:discord.User = None):
msg = ''
for a in user.roles:
msg+= a.name
Have you tried
channel = await self.bot.get_channel(int(val))