7 changed files with 112 additions and 123 deletions
@ -1,23 +1,28 @@ |
|||||
import discord |
import discord |
||||
import asyncio |
import asyncio |
||||
|
|
||||
client = discord.Client() |
class MyClient(discord.Client): |
||||
|
def __init__(self, *args, **kwargs): |
||||
|
super().__init__(*args, **kwargs) |
||||
|
|
||||
async def my_background_task(): |
# create the background task and run it in the background |
||||
await client.wait_until_ready() |
self.bg_task = self.loop.create_task(self.my_background_task()) |
||||
counter = 0 |
|
||||
channel = discord.Object(id='channel_id_here') |
|
||||
while not client.is_closed: |
|
||||
counter += 1 |
|
||||
await client.send_message(channel, counter) |
|
||||
await asyncio.sleep(60) # task runs every 60 seconds |
|
||||
|
|
||||
@client.event |
async def on_ready(self): |
||||
async def on_ready(): |
print('Logged in as') |
||||
print('Logged in as') |
print(self.user.name) |
||||
print(client.user.name) |
print(self.user.id) |
||||
print(client.user.id) |
print('------') |
||||
print('------') |
|
||||
|
|
||||
client.loop.create_task(my_background_task()) |
async def my_background_task(self): |
||||
|
await self.wait_until_ready() |
||||
|
counter = 0 |
||||
|
channel = self.get_channel(1234567) # channel ID goes here |
||||
|
while not self.is_closed: |
||||
|
counter += 1 |
||||
|
await channel.send(counter) |
||||
|
await asyncio.sleep(60) # task runs every 60 seconds |
||||
|
|
||||
|
|
||||
|
client = MyClient() |
||||
client.run('token') |
client.run('token') |
||||
|
@ -1,22 +1,21 @@ |
|||||
import discord |
import discord |
||||
|
|
||||
client = discord.Client() |
class MyClient(discord.Client): |
||||
|
async def on_ready(self): |
||||
|
print('Connected!') |
||||
|
print('Username: {0.name}\nID: {0.id}'.format(self.user)) |
||||
|
|
||||
@client.event |
async def on_message(self, message): |
||||
async def on_ready(): |
if message.content.startswith('!deleteme'): |
||||
print('Connected!') |
msg = await message.channel.send('I will delete myself now...') |
||||
print('Username: ' + client.user.name) |
await msg.delete() |
||||
print('ID: ' + client.user.id) |
|
||||
|
|
||||
@client.event |
# this also works |
||||
async def on_message(message): |
await message.channel.send('Goodbye in 3 seconds...', delete_after=3.0) |
||||
if message.content.startswith('!deleteme'): |
|
||||
msg = await client.send_message(message.channel, 'I will delete myself now...') |
|
||||
await client.delete_message(msg) |
|
||||
|
|
||||
@client.event |
async def on_message_delete(self, message): |
||||
async def on_message_delete(message): |
fmt = '{0.author} has deleted the message: {0.content}' |
||||
fmt = '{0.author.name} has deleted the message:\n{0.content}' |
await message.channel.send(fmt.format(message)) |
||||
await client.send_message(message.channel, fmt.format(message)) |
|
||||
|
|
||||
|
client = MyClient() |
||||
client.run('token') |
client.run('token') |
||||
|
@ -1,24 +1,20 @@ |
|||||
import discord |
import discord |
||||
import asyncio |
import asyncio |
||||
|
|
||||
client = discord.Client() |
class MyClient(discord.Client): |
||||
|
async def on_ready(self): |
||||
|
print('Connected!') |
||||
|
print('Username: {0.name}\nID: {0.id}'.format(self.user)) |
||||
|
|
||||
@client.event |
async def on_message(self, message): |
||||
async def on_ready(): |
if message.content.startswith('!editme'): |
||||
print('Connected!') |
msg = await message.channel.send('10') |
||||
print('Username: ' + client.user.name) |
await asyncio.sleep(3.0) |
||||
print('ID: ' + client.user.id) |
await msg.edit(content='40') |
||||
|
|
||||
@client.event |
async def on_message_edit(self, before, after): |
||||
async def on_message(message): |
fmt = '**{0.author}** edited their message:\n{0.content} -> {1.content}' |
||||
if message.content.startswith('!editme'): |
await before.channel.send(fmt.format(before, after)) |
||||
msg = await client.send_message(message.author, '10') |
|
||||
await asyncio.sleep(3) |
|
||||
await client.edit_message(msg, '40') |
|
||||
|
|
||||
@client.event |
|
||||
async def on_message_edit(before, after): |
|
||||
fmt = '**{0.author}** edited their message:\n{1.content}' |
|
||||
await client.send_message(after.channel, fmt.format(after, before)) |
|
||||
|
|
||||
|
client = MyClient() |
||||
client.run('token') |
client.run('token') |
||||
|
@ -1,37 +1,32 @@ |
|||||
import discord |
import discord |
||||
import random |
import random |
||||
|
|
||||
client = discord.Client() |
class MyClient(discord.Client): |
||||
|
async def on_ready(self): |
||||
@client.event |
print('Logged in as') |
||||
async def on_message(message): |
print(self.user.name) |
||||
# we do not want the bot to reply to itself |
print(self.user.id) |
||||
if message.author == client.user: |
print('------') |
||||
return |
|
||||
|
async def on_message(self, message): |
||||
if message.content.startswith('$guess'): |
# we do not want the bot to reply to itself |
||||
await client.send_message(message.channel, 'Guess a number between 1 to 10') |
if message.author.id == self.user.id: |
||||
|
|
||||
def guess_check(m): |
|
||||
return m.content.isdigit() |
|
||||
|
|
||||
guess = await client.wait_for_message(timeout=5.0, author=message.author, check=guess_check) |
|
||||
answer = random.randint(1, 10) |
|
||||
if guess is None: |
|
||||
fmt = 'Sorry, you took too long. It was {}.' |
|
||||
await client.send_message(message.channel, fmt.format(answer)) |
|
||||
return |
return |
||||
if int(guess.content) == answer: |
|
||||
await client.send_message(message.channel, 'You are right!') |
|
||||
else: |
|
||||
await client.send_message(message.channel, 'Sorry. It is actually {}.'.format(answer)) |
|
||||
|
|
||||
|
if message.content.startswith('$guess'): |
||||
|
await message.channel.send('Guess a number between 1 and 10.') |
||||
|
check = lambda m: m.content.isdigit() |
||||
|
guess = await self.wait_for_message(author=message.author, check=check, timeout=5.0) |
||||
|
|
||||
|
answer = random.randint(1, 10) |
||||
|
if guess is not None: |
||||
|
await message.channel.send('Sorry, you took too long it was {}.'.format(answer)) |
||||
|
return |
||||
|
|
||||
@client.event |
if int(guess.content) == answer: |
||||
async def on_ready(): |
await message.channel.send('You are right!') |
||||
print('Logged in as') |
else: |
||||
print(client.user.name) |
await message.channel.send('Oops. It is actually {}.'.format(answer)) |
||||
print(client.user.id) |
|
||||
print('------') |
|
||||
|
|
||||
|
client = MyClient() |
||||
client.run('token') |
client.run('token') |
||||
|
@ -1,18 +1,15 @@ |
|||||
import discord |
import discord |
||||
|
|
||||
client = discord.Client() |
class MyClient(discord.Client): |
||||
|
async def on_ready(self): |
||||
@client.event |
print('Logged in as') |
||||
async def on_member_join(member): |
print(self.user.name) |
||||
server = member.server |
print(self.user.id) |
||||
fmt = 'Welcome {0.mention} to {1.name}!' |
print('------') |
||||
await client.send_message(server, fmt.format(member, server)) |
|
||||
|
async def on_member_join(self, member): |
||||
@client.event |
guild = member.guild |
||||
async def on_ready(): |
await guild.default_channel.send('Welcome {0.mention} to {1.name}!'.format(member, guild)) |
||||
print('Logged in as') |
|
||||
print(client.user.name) |
client = MyClient() |
||||
print(client.user.id) |
|
||||
print('------') |
|
||||
|
|
||||
client.run('token') |
client.run('token') |
||||
|
@ -1,22 +1,19 @@ |
|||||
import discord |
import discord |
||||
|
|
||||
client = discord.Client() |
class MyClient(discord.Client): |
||||
|
async def on_ready(self): |
||||
|
print('Logged in as') |
||||
|
print(self.user.name) |
||||
|
print(self.user.id) |
||||
|
print('------') |
||||
|
|
||||
@client.event |
async def on_message(self, message): |
||||
async def on_message(message): |
# we do not want the bot to reply to itself |
||||
# we do not want the bot to reply to itself |
if message.author.id == self.user.id: |
||||
if message.author == client.user: |
return |
||||
return |
|
||||
|
|
||||
if message.content.startswith('!hello'): |
if message.content.startswith('!hello'): |
||||
msg = 'Hello {0.author.mention}'.format(message) |
await message.channel.send('Hello {0.author.mention}'.format(message)) |
||||
await client.send_message(message.channel, msg) |
|
||||
|
|
||||
@client.event |
|
||||
async def on_ready(): |
|
||||
print('Logged in as') |
|
||||
print(client.user.name) |
|
||||
print(client.user.id) |
|
||||
print('------') |
|
||||
|
|
||||
|
client = MyClient() |
||||
client.run('token') |
client.run('token') |
||||
|
Loading…
Reference in new issue