Basically whenever I run this code it gives me an error, I am trying to make the code server mute one of my friends every 20 seconds for 5 seconds, it says there is something wrong with the line where the user is being defined.
import discord
from discord.ext import commands
from discord.ext import commands, tasks
import random
import datetime
from datetime import date
import calendar
import time
import asyncio
from discord.ext.tasks import loop
client = commands.Bot(command_prefix='.')
#tasks.loop(seconds=20)
async def mute_person():
user = await discord.Guild.fetch_member("670549896247509002", "670549896247509002") # Get the Member Object
await user.edit(mute=True) # Mute
await asyncio.sleep(20) # Waits for 20 seconds then unmute.
await user.edit(mute=False) # Unmute
#client.event
async def on_ready():
print("I am ready")
mute_person.start()
client.run("Token")
You are not using the right function discord.Guild doesn't exist as you are using it. the main use must be await guild.fetch_member(member_id) and then guild means the guild object which has a specific ID as well. So you need to get the guild first.
Related
So, I have created a command that executes a reoccurring message. I have it set to 10 seconds in the code for testing purposes, but I will have the message loop every 4 hours. I don't get any errors when running the code, but the command is not actually executing. The message never sends and I am confused as to why and can't seem to figure it out.
import discord
import discord.utils
import asyncio
import http.client
import aiohttp
import EZPaginator
import platform
import yaml
import os
from yaml import load, dump
from discord.ext import commands
from discord.ext import tasks
intents = discord.Intents.default()
client = commands.Bot(command_prefix = '$', intents=intents)
client.remove_command('help')
#client.event
async def on_ready():
print('ZOF BOT IS ONLINE!')
# Reoccuring Message Reminder
#tasks.loop(seconds=10)
async def send_message(ctx):
channel = discord.utils.get(ctx.guild.channels, name='reminders')
remind = discord.Embed(title='**:rotating_light:ATTENTION ZOF:', description='Do not forget to donate to Fortress, Alliance tech, and Ruins!', color=0xFFFFFF)
print('Reoccurring Message Sent.')
await channel.send(embed=remind) ```
You need to start you task function for example in on_ready event:
#client.event
async def on_ready():
if not send_message.is_running():
send_message.start()
How can I send a message every x time with a discord bot with discord.py?
Here is my code:
import discord
from discord.ext import commands
from discord.ext import tasks
import os
TOKEN = 'xxxxxxxxxxx'
bot = commands.Bot(command_prefix='!',help_command=None,activity=discord.Game(name="!help"))
#tasks.loop(seconds=5)
async def ping(channel):
print("is the function being called")
channel = bot.get_channel(877115461014282320)
await channel.send('embed=embed')
bot.run(TOKEN)
Edit: I know I shouldn't leak my key but I edited it after 10 seconds and removed a letter why could someone still access it?
You have to start task using ping.start() in event on_ready or in some command.
import discord
from discord.ext import commands
from discord.ext import tasks
import os
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='!', help_command=None, activity=discord.Game(name="!help"))
#tasks.loop(seconds=5)
async def ping():
print("is the function being called")
#channel = bot.get_channel(877115461014282320)
#await channel.send('embed=embed')
#bot.event
async def on_ready():
print('on ready')
ping.start()
bot.run(TOKEN)
You have also ping.stop(), ping.cancel(), etc. See doc: tasks
I am not able to change the status. I want to develop my own bot and have to add more features too
# Importing Modules
import discord
import asyncio
import random
import DateTime
from discord.ext import commands
from urllib import parse, request
import re
import time
# Importants
bot = commands.Bot(command_prefix='*', description="This is a Helper Bot")
client = discord.Client()
# ***ADDING FEATURES***
# Getting Online And Changing status
async def my_background_task():
await client.wait_until_ready()
# Setting `Playing ` status
await bot.change_presence(activity=discord.Game(name="With Your Life"))
time.sleep(5)
# Setting `Streaming ` status
await bot.change_presence(activity=discord.Streaming(name="Kartik Jain", url="https://youtube.com/c/KartikJainChannel"))
time.sleep(5)
# Setting `Listening ` status
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="To *help"))
time.sleep(5)
# Setting `Watching ` status
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=" Your Nonsense"))
time.sleep(5)
# While Loop To loop changing statuses
while not bot.is_closed():
bot.bg_task = bot.loop.create_task(my_background_task())
# Making the bot online
client.run('My_ID')
You have to change the bot with client and you have to change this my_background_task() to this: on_ready().
Just look at this code:
import discord
import asyncio
import random
import DateTime
from discord.ext import commands
from urllib import parse, request
import re
import time
# Importants
bot = commands.Bot(command_prefix='*', description="This is a Helper Bot")
client = discord.Client()
# ***ADDING FEATURES***
# Getting Online And Changing status
#client.event
async def on_ready():
await client.change_presence(activity=discord.Streaming(name="My live",url="http://www.twitch.tv/G4merStop"))
time.sleep(5)
await client.change_presence(activity=discord.Streaming(name="Your live",url="http://www.twitch.tv/G4merStop"))
time.sleep(5)
# ^^^^ here you can see what you can do in the docs ^^^^
# Making the bot online
client.run('My_ID')```
First of all, you're actually creating 2 different instances of the client, client using discord.Client and bot using commands.Bot. You're using bot to change the presence, and in the end, you're running the client instance, which makes part done using bot totally useless.
You don't need to use discord.Client, because commands.Bot has all the methods and attributes of discord.Client.
Next, you should change time.sleep() to await asyncio.sleep() because it's blocking your code and also put the bot.loop.create_task() method inside on_ready event
I'm trying to make this code work only in a specific channel. It just sends a ton of errors when I try to do the command in the right channel. Ill add my imports if you want to test it. Last time I tried to ask this question it got denied. I still don't know what to do and just need a little help as i'm new to coding a discord bot.
import discord
from discord.ext import commands, tasks
import os
import random
import asyncio
from asyncio import gather
client = commands.Bot(command_prefix='.')
#client.command()
async def car(ctx):
pictures = [
'https://car-images.bauersecure.com/pagefiles/78294/la_auto_show_11.jpg',
'http://www.azstreetcustom.com/uploads/2/7/8/9/2789892/az-street-custom-gt40-2_orig.jpg',
'http://tenwheel.com/imgs/a/b/l/t/z/1967_firebird_1968_69_70_2000_camaro_blended_custom_supercharged_street_car_1_lgw.jpg',
'https://rthirtytwotaka.files.wordpress.com/2013/06/dsc_0019.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2008/06/fluke27.jpg',
'https://i.ytimg.com/vi/pCt0KXC1tng/maxresdefault.jpg',
'https://i2.wp.com/www.tunedinternational.com/featurecars/dorift/02.jpg',
'http://i.imgur.com/nEbyV82.jpg',
'https://cdn.hiconsumption.com/wp-content/uploads/2019/02/Affordable-Vintage-Japanese-Cars-0-Hero-1087x725.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2012/04/IMG_0268.jpg',
'https://i.ytimg.com/vi/Y-moGXK2zLk/maxresdefault.jpg',
'https://www.topgear.com/sites/default/files/images/big-read/carousel/2016/03/568cd4ab437c6557c583a6f4a4feb6d1/3carguyscarouselmarch2016.jpg'
]
channel = discord.utils.get()
if channel == 705161333972140072:
await ctx.channel.purge(limit=1)
await ctx.send(f'{random.choice(pictures)}')
client.run('token')
You need a few changes in the command function:
Fix indentation
Use ctx.channel.id instead of channeland discord.utils.get()
Rather than purging, delete the command msg
#client.command()
async def car(ctx):
pictures = [
'https://car-images.bauersecure.com/pagefiles/78294/la_auto_show_11.jpg',
'http://www.azstreetcustom.com/uploads/2/7/8/9/2789892/az-street-custom-gt40-2_orig.jpg',
'http://tenwheel.com/imgs/a/b/l/t/z/1967_firebird_1968_69_70_2000_camaro_blended_custom_supercharged_street_car_1_lgw.jpg',
'https://rthirtytwotaka.files.wordpress.com/2013/06/dsc_0019.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2008/06/fluke27.jpg',
'https://i.ytimg.com/vi/pCt0KXC1tng/maxresdefault.jpg',
'https://i2.wp.com/www.tunedinternational.com/featurecars/dorift/02.jpg',
'http://i.imgur.com/nEbyV82.jpg',
'https://cdn.hiconsumption.com/wp-content/uploads/2019/02/Affordable-Vintage-Japanese-Cars-0-Hero-1087x725.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2012/04/IMG_0268.jpg',
'https://i.ytimg.com/vi/Y-moGXK2zLk/maxresdefault.jpg',
'https://www.topgear.com/sites/default/files/images/big-read/carousel/2016/03/568cd4ab437c6557c583a6f4a4feb6d1/3carguyscarouselmarch2016.jpg'
]
if ctx.channel.id == 705161333972140072:
await ctx.message.delete()
await ctx.send(random.choice(pictures))
client.run('token')
You can also use checks
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from PIL import Image
Client = discord.Client()
client = commands.Bot(command_prefix = "-")`
#client.command
async def spam(ctx, arg):
count = 0
await Context.send(message.channel, "Wait for it....")
time.sleep(3)
while count < 20:
await ctx.send(arg)
time.sleep(3)
count = count + 1
This code is supposed to mention the person specified in the argument. For example if someone typed -spam #Bob the bot should say
#Bob
#Bob 20 times
There's no need to instantiate both discord.Client and commands.Bot. Bot is a subclass of Client.
Bot.command is a function that returns a decorator, not itself a decorator. You need to call it to use it to decorate a coroutine:
#client.command()
You should probably be using a converter to acquire the user you're pinging.
Context is the class that ctx is an instance of. You should be accessing all of the methods through ctx.
Don't use time.sleep, as it blocks the event loop. Await asyncio.sleep instead.
from discord.ext.commands import Bot
from asyncio import sleep
from discord import User
client = Bot(command_prefix = "-")
#client.command()
async def spam(ctx, user: User):
await ctx.send("Wait for it....")
await sleep(3)
for _ in range(20):
await ctx.send(user.mention)
await sleep(3)
client.run("token")